package com.fan.gofast.compent;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

/**
 * 可以记录业务操作日志的CRUD(增、删、改)
 * @param <M>
 */
@Repository
public class CrudRepository<M> {
    protected Log log = LogFactory.getLog(getClass());


    /**
     * 仅记录修改后的内容
     * @param entity
     * @param entityClazz
     * @param type
     * @return
     */
    @LogRecord(
            operator = "{{#entity.updateByName}}",
            success = "修改:{{#type}}内容为「{{T(com.alibaba.fastjson.JSON).toJSONString(#entity)}}」,操作结果:{{#_ret}}",
            extra = "{{T(com.alibaba.fastjson.JSON).toJSONString(#entity)}}",
            type = "{{#type}}", bizNo = "{{#entity.id}}")
    public boolean updateById(M entity, Class<M> entityClazz,String type) {
        SqlSession sqlSession = SqlHelper.sqlSession(entityClazz);
        BaseMapper<M> mapper = SqlHelper.getMapper(entityClazz, sqlSession);
        return SqlHelper.retBool(mapper.updateById(entity));
    }

    /**
     * 记录修改前的内容，和修改后的内容
     * @param entity
     * @param oldEntity
     * @param entityClazz
     * @param type
     * @return
     */
    @LogRecord(
            operator = "{{#entity.updateByName}}",
            success = "修改:{{#type}}内容为「{{T(com.alibaba.fastjson.JSON).toJSONString(#entity)}}」,操作结果:{{#_ret}}",
            extra = "oldEntity:{{T(com.alibaba.fastjson.JSON).toJSONString(#oldEntity)}},newEntity:{{T(com.alibaba.fastjson.JSON).toJSONString(#entity)}}",
            type = "{{#type}}", bizNo = "{{#entity.id}}")
    public boolean updateById(M entity,M oldEntity,Class<M> entityClazz,String type) {
        SqlSession sqlSession = SqlHelper.sqlSession(entityClazz);
        BaseMapper<M> mapper = SqlHelper.getMapper(entityClazz, sqlSession);
        return SqlHelper.retBool(mapper.updateById(entity));
    }

    /**
     * 记录新增的内容
     * @param entity
     * @param entityClazz
     * @param type
     * @return
     */
    @LogRecord(
            operator = "{{#entity.createByName}}",
            success = "新增:"+"{{#type}}"+",新增内容:「{{T(com.alibaba.fastjson.JSON).toJSONString(#entity)}}」,操作结果:{{#_ret}}",
            extra = "{{T(com.alibaba.fastjson.JSON).toJSONString(#entity)}}",
            type = "{{#type}}", bizNo = "{{#entity.id}}")
    public boolean save(M entity,Class<M> entityClazz,String type) {
        SqlSession sqlSession = SqlHelper.sqlSession(entityClazz);
        BaseMapper<M> mapper = SqlHelper.getMapper(entityClazz, sqlSession);
        return SqlHelper.retBool(mapper.insert(entity));
    }

    /**
     * 记录删除的内容
     * @param entity
     * @param entityClazz
     * @param type
     * @return
     */
    @LogRecord(
            operator = "{{#entity.updateByName}}",
            success = "删除:{{#type}},删除内容:「{{T(com.alibaba.fastjson.JSON).toJSONString(#entity)}}」,操作结果:{{#_ret}}",
            extra = "{{T(com.alibaba.fastjson.JSON).toJSONString(#entity)}}",
            type = "{{#type}}", bizNo = "{{#entity.id}}")
    public boolean deleteById(M entity, Class<M> entityClazz,String type) {
        SqlSession sqlSession = SqlHelper.sqlSession(entityClazz);
        BaseMapper<M> mapper = SqlHelper.getMapper(entityClazz, sqlSession);
        return SqlHelper.retBool(mapper.updateById(entity));
    }

}
